package com.zfl.service.impl;

import com.zfl.mapper.UserMapper;
import com.zfl.pojo.User;
import com.zfl.service.UserService;
import com.zfl.utils.Argon2PasswordUtil;
import com.zfl.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public void register(String username, String password) {
        //加密
        String encryptedPassword = Argon2PasswordUtil.encryptPassword(password);
        Long time = System.currentTimeMillis();
        //添加
        userMapper.add(username, encryptedPassword, time, time);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(System.currentTimeMillis());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        userMapper.updateAvatar(avatarUrl, System.currentTimeMillis(), id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        long updateTime = System.currentTimeMillis();
        userMapper.updatePwd(Argon2PasswordUtil.encryptPassword(newPwd),updateTime,id);
    }
}
